package io.gitee.tuo_tuo.jfinaldevassistance.util;

import java.io.IOException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 描述:
 * 版权: Copyright (c) 2018
 * 作者: tuotuo
 * 版本: 1.0
 * 创建日期: 2018/8/7
 * 创建时间: 13:22
 */
public class JarUtil {

	/**
	 * 从pom文件获取jar的版本
	 * @param pomPath  eg: jfinal  "/META-INF/maven/com.jfinal/jfinal/pom.properties"
	 * @return
	 * @throws IOException
	 */
	public static String getVersionFromPom(String pomPath) throws IOException {
		Properties properties = new Properties();
		properties.load(JarUtil.class.getResourceAsStream(pomPath));
		return properties.get("version").toString();
	}

	/**
	 * 从jar文件名获取jar的版本
	 * @param clazz  jar中任意类的class
	 * @return
	 * @throws IOException
	 */
	public static String getVersionFromJarName(Class clazz) throws IOException {
		String jarVersion = null;
		String jarFileName = clazz.getProtectionDomain().getCodeSource().getLocation().getFile();
		Pattern pattern = Pattern.compile("-(.+)\\.jar");
		Matcher matcher = pattern.matcher(jarFileName);
		if(matcher.find()){
			jarVersion = matcher.group(1);
		}
		return jarVersion;
	}
}
